﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class PlatForm_MasterPage : System.Web.UI.MasterPage
{
	/// <summary>
	/// 专家控制类对象
	/// </summary>
	private PX.Expert.ExpertComponent clsExpertComp = new PX.Expert.ExpertComponent();		// 专家控制类对象
	/// <summary>
	/// 用户控制类
	/// </summary>
	private PX.User.UserComponent clsUserComp = new PX.User.UserComponent();		// 用户控制类
	/// <summary>
	/// 用户组控制类
	/// </summary>
	private PX.UserGroup.UserGroupComponent clsUserGroupComp = new PX.UserGroup.UserGroupComponent();		// 用户组控制类
	/// <summary>
	/// 提问者控制类
	/// </summary>
	private PX.Asker.Component clsAskerComp = new PX.Asker.Component();		// 提问者控制类

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		clsUserComp.page = Page;

		LBtnLogin.Click += new EventHandler(LBtnLogin_Click);
		LBtnLogout.Click += new EventHandler(LBtnLogout_Click);
		
	}

	/// <summary>
	/// 注销按钮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnLogout_Click(object sender, EventArgs e)
	{
		//Session["AskerUserID"] = 0;
		Session.Abandon();
		Session.Clear();
		CheckAskerIsLogin();
		Response.Redirect(Request.Url.ToString());
	}

	/// <summary>
	/// 提问者登录按钮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnLogin_Click(object sender, EventArgs e)
	{
		PX.User.User clsUser = new PX.User.User();
		PX.Asker.Entity clsAsker = new PX.Asker.Entity();

		if (clsUserComp.CheckLogin(TbUsername.Text, TbPassword.Text, ref clsUser))
		{
			
			clsAsker = clsAskerComp.Query(clsUser.ID);

			if (clsAsker != null)
			{

				if (clsAsker.IsAnoynmous)
				{
					Common.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
				}
				else
				{
					Session["AskerUserID"] = clsUser.ID.ToString();
					Session["LoginUserID"] = clsUser.ID;
					Session["LoginUserRight"] = clsUserGroupComp.GetOne(clsUser.UserGroupID).ColumnIDs;
					Session["LoginUserDepartmentID"] = clsUser.DepartmentID;
				}
			}
			else
			{
				Common.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
				//Common.ShowMessageAlert.ShowMessage("用户名或密码错！");
			}
		}
		else
		{
			Common.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
		}

		CheckAskerIsLogin();
		//Response.Redirect(Request.Url.ToString());
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		CheckAskerIsLogin();


		ShowExperts();
	}

	/// <summary>
	/// 显示所有专家
	/// </summary>
	private void ShowExperts()
	{
		List<PX.Expert.Expert> listExpertOne = new List<PX.Expert.Expert>();		// 博士后专家
		List<PX.Expert.Expert> listExpertTwo = new List<PX.Expert.Expert>();		// 技能大师
		List<PX.Expert.Expert> listExpert = new List<PX.Expert.Expert>();		// 所有专家

		listExpertOne = clsExpertComp.QueryExpertOne();
		listExpertTwo = clsExpertComp.QueryExpertTwo();

		for (int i = 0; i < listExpertOne.Count; i++)
		{
			listExpert.Add(listExpertOne[i]);
		}

		for (int i = 0; i < listExpertTwo.Count; i++)
		{
			listExpert.Add(listExpertTwo[i]);
		}

		for (int i = 0; i < listExpert.Count; i++)
		{
			Panel panelOneExpert = new Panel();
			Panel panelExpertName = new Panel();
			Panel panelExpertImage = new Panel();
			Panel panelExpertQuestion = new Panel();
			Label labelExpertName = new Label();
			Image imageExpertImage = new Image();
			HyperLink hLinkIntroduction = new HyperLink();
			HyperLink hLinkQuestion = new HyperLink();
			Label labSplit = new Label();

			panelOneExpert.CssClass = "OneExpert";
			panelExpertName.CssClass = "ExpertName";
			panelExpertImage.CssClass = "DivImage";
			panelExpertQuestion.CssClass = "DivQuestion";
			imageExpertImage.CssClass = "Icon";
			labSplit.CssClass = "Split";

			hLinkIntroduction.Text = "简介";
			hLinkQuestion.Text = "提问";
			labSplit.Text = "|";

			hLinkIntroduction.NavigateUrl = "Introduction.aspx?ID=" + listExpert[i].ID;
			hLinkQuestion.NavigateUrl = "Ask.aspx?ExpertID=" + listExpert[i].ID;

			labelExpertName.Text = listExpert[i].TypeName + "：" + listExpert[i].Name;
			panelExpertName.Controls.Add(labelExpertName);

			imageExpertImage.ImageUrl = "~/ShowImage.aspx?FileID=" + listExpert[i].ImageID.ToString();
			panelExpertImage.Controls.Add(imageExpertImage);

			panelExpertQuestion.Controls.Add(hLinkIntroduction);
			//panelExpertQuestion.Controls.Add(labSplit);
			panelExpertQuestion.Controls.Add(hLinkQuestion);

			panelOneExpert.Controls.Add(panelExpertName);
			panelOneExpert.Controls.Add(panelExpertImage);
			panelOneExpert.Controls.Add(panelExpertQuestion);

			PanelExperts.Controls.Add(panelOneExpert);
		}

	}

	/// <summary>
	/// 检查是否已经有提问者登录，有登录提问者显示已经登录视图，没有显示登录视图
	/// </summary>
	private void CheckAskerIsLogin()
	{
		int nLoginAskerUserID;
		PX.User.User clsLoginAskerUser = new PX.User.User();	// 登录的提问者
		PX.Asker.Entity clsLoginAsker = new PX.Asker.Entity();

		try
		{
			nLoginAskerUserID = Convert.ToInt32(Session["AskerUserID"]);
		}
		catch
		{
			nLoginAskerUserID = 0;
		}

		if (nLoginAskerUserID != 0)
		{
			
			clsLoginAskerUser = clsUserComp.GetOne(nLoginAskerUserID);
			clsLoginAsker = clsAskerComp.Query(clsLoginAskerUser.ID);

			if (clsLoginAsker.IsAnoynmous)
			{
				MViewAsker.SetActiveView(ViewNoLogin);
				//Common.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
			}
			else
			{
				Session["LoginUserID"] = nLoginAskerUserID;
				MViewAsker.SetActiveView(ViewLogin);

				LabAskerLogin.Text = "欢迎" + clsLoginAskerUser.Username + "回来！";
			}

		}
		else
		{
			MViewAsker.SetActiveView(ViewNoLogin);
		}
	}
}
